package com.yztc.ui.classifydetail.myadapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.yztc.bean.classifydetail.ClassifyProductListBean;
import com.yztc.chunboapp.R;
import com.yztc.ui.classifydetail.ClassifyDetailActivity;
import com.yztc.utils.ImageHelper;

import java.util.List;

/**
 * Created by MengLingzhang on 2016/10/11.
 */
public class ClassifyDetailProductAdapter extends RecyclerView.Adapter<ClassifyDetailProductAdapter.MyViewHolder>{
    private Context context;
    private List<ClassifyProductListBean> list;

    //将定义事件的接口作为适配器的属性
    private ClassifyDetailActivity.onItemClickListener onItemClickListener;

    public void setOnItemClickListener(ClassifyDetailActivity.onItemClickListener onItemClickListener){
        this.onItemClickListener=onItemClickListener;
    }

    public ClassifyDetailProductAdapter(Context context,List<ClassifyProductListBean> list){
        this.context=context;
        this.list=list;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        MyViewHolder holder=new MyViewHolder(LayoutInflater.from
                (context).inflate(R.layout.item_classify_detail_product,parent,false));
        return holder;
    }

    @Override
    public void onBindViewHolder(final MyViewHolder holder, int position) {
        if(list.get(position).getImageUrl()!=null && list.get(position).getImageUrl()!=""){
            ImageHelper.getNormalImage(context,list.get(position).getImageUrl(),holder.iv);
        }else {
        }
        holder.tv01.setText(list.get(position).getShortname());
        holder.tv02.setText(list.get(position).getSubname());
        holder.tv03.setText(list.get(position).getSpecifications());
        holder.tv04.setText("¥"+(int) list.get(position).getOrderPrice1());
        holder.tv05.setText("¥"+(int) list.get(position).getChunboPrice1());

        //判断当前控件是否设置了监听
        if(onItemClickListener!=null){
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int pos=holder.getLayoutPosition();
                    onItemClickListener.onItemClick(holder.itemView,pos-1);
                }
            });

            holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    int pos=holder.getLayoutPosition();
                    onItemClickListener.onItemLongClick(holder.itemView,pos-1);
                    return false;
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder{
        ImageView iv;
        TextView tv01,tv02,tv03,tv04,tv05;
        public MyViewHolder(View itemView) {
            super(itemView);
            iv= (ImageView) itemView.findViewById(R.id.iv_item_classify_prouct);
            tv01= (TextView) itemView.findViewById(R.id.tv01_item_classify_prouct);
            tv02= (TextView) itemView.findViewById(R.id.tv02_item_classify_prouct);
            tv03= (TextView) itemView.findViewById(R.id.tv03_item_classify_prouct);
            tv04= (TextView) itemView.findViewById(R.id.tv04_item_classify_prouct);
            tv05= (TextView) itemView.findViewById(R.id.tv05_item_classify_prouct);
        }
    }
}